package org.hikari.qqbot.xiuxian.command;

import lombok.Getter;
import lombok.Setter;
import org.hikari.qqbot.xiuxian.util.MessageUtil;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * created at 2024-12-16 01:55:36
 *
 * @author yinchao
 * @version 1.0
 **/
@Getter
@Setter
public class ExecutableCommand implements Delayed {

    /**
     * 任务id
     */
    private String taskId;
    /**
     * 发送的命令文本
     */
    private String command;
    /**
     * 发送时间
     */
    private Date sendTime;
    /**
     * 当前用户id
     */
    private Long selfId;
    /**
     * 发送用户id
     */
    private Long userId;
    /**
     * 发送QQ群id
     */
    private Long groupId;
    /**
     * 回调函数
     */
    private ICommandSendCallback callback;

    public ExecutableCommand(String taskId, String command, Long selfId, Long userId, Long groupId, Date sendTime) {
        this(taskId, command, selfId, userId, groupId, sendTime, null);
    }

    public ExecutableCommand(String taskId, String command, Long selfId, Long userId, Long groupId, Date sendTime, ICommandSendCallback callback) {
        this.taskId = Objects.requireNonNull(taskId, "Param 'taskId' must not be null");
        this.command = Objects.requireNonNull(command, "Param 'command' must not be null");
        this.selfId = Objects.requireNonNull(selfId, "Param 'selfId' must not be null");
        this.userId = Objects.requireNonNull(userId, "Param 'userId' must not be null");
        this.groupId = groupId;
        this.sendTime = sendTime;
        this.callback = callback;
    }

    @Override
    public String toString() {
        // [用户 - 990652186] 命令[打坐] => 修仙之路(10000)
        // [用户 - 990652186] 命令[@12345 打坐] => QQ群(10000) 预计发送时间[2024年10月11日 12点13分14秒]
        String sendTimeStr = sendTime == null ? "" : "预计发送时间[" + MessageUtil.getDisplaySendTime(sendTime) + "]";
        if (groupId == null) {
            return "用户[" + selfId + "] 命令[" + command + "] => 修仙之路(" + userId + ") " + sendTimeStr;
        }
        return "用户[" + selfId + "] 命令[@" + userId + " " + command + "] => QQ群(" + groupId + ") " + sendTimeStr;

    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(getSendTime().getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return Long.compare(getDelay(TimeUnit.NANOSECONDS), o.getDelay(TimeUnit.NANOSECONDS));
    }

}
